package com.example.demo.service;

import com.example.demo.dao.TBizArtDAO;
import com.example.demo.po.PageRequest;
import com.example.demo.po.PageResult;
import com.example.demo.po.TBizArt;
import com.example.demo.po.UserManage;
import com.example.demo.util.DateUtil;
import com.example.demo.util.PageUtils;
import com.example.demo.util.SeqFactory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class TBizArtService {
    @Autowired
    TBizArtDAO tBizArtDAO;

    /**
     * 作品分页查询接口
     * 这里统一封装了分页请求和结果，避免直接引入具体框架的分页对象, 如MyBatis或JPA的分页对象
     * 从而避免因为替换ORM框架而导致服务层、控制层的分页接口也需要变动的情况，替换ORM框架也不会
     * 影响服务层以上的分页接口，起到了解耦的作用
     * @param pageRequest 自定义，统一分页查询请求
     * @return PageResult 自定义，统一分页查询结果
     */
    public PageResult findByPage(TBizArt art, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TBizArt> list = tBizArtDAO.list(art);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizArt>(list));
        return pageResult;
    }


    /**
     * 根据拍卖ID查询作品信息
     * @param auctionid
     * @param pageRequest
     * @return
     */
    public PageResult findByPage(String auctionid, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TBizArt> list = tBizArtDAO.listByAuc(auctionid);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizArt>(list));
        return pageResult;
    }

    /**
     * 获取未绑定拍卖会的作品
     * @param pageRequest
     * @return
     */
    public PageResult findNoauc(TBizArt art, PageRequest pageRequest){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        System.out.println(art.getStyle());
        System.out.println(art.getName());
        PageHelper.startPage(pageNum, pageSize);
        List<TBizArt> list = tBizArtDAO.findNoauc(art);
        PageResult pageResult = PageUtils.getPageResult(pageRequest, new PageInfo<TBizArt>(list));
        return pageResult;
    }



    public boolean saveOrUpdtae(TBizArt art, HttpServletRequest request){
        UserManage user =(UserManage)request.getSession().getAttribute("session_user");
        int i = 0;
        if(art.getId()!=null&&!"".equals(art.getId())) {
            art.setUpdatetime(DateUtil.formatDate(new Date()));
            art.setUpdateuser(user.getId().toString());
            i = tBizArtDAO.update(art);
        }else {
            String artCode ="A"+ SeqFactory.getNewSequenceAlone();
            art.setArtcode(artCode);

            art.setCttime(DateUtil.formatDate(new Date()));
            art.setCtteuser(user.getId().toString());
            i = tBizArtDAO.save(art);
        }
        System.out.println(i);
        if(i>0)
            return true;
        else
            return false;
    }

    public boolean delete(String id, HttpServletRequest request){
        UserManage user =(UserManage)request.getSession().getAttribute("session_user");
        TBizArt art = new TBizArt();
        art.setId(Integer.valueOf(id));
        art.setSalestate("4");
        art.setUpdatetime(DateUtil.formatDate(new Date()));
        art.setUpdateuser(user.getId().toString());
        int i = tBizArtDAO.update(art);
        if(i>0)
            return true;
        else
            return false;
    }

    public TBizArt findById(Integer id){
        TBizArt art = tBizArtDAO.findById(id);
        return  art;
    }

    public HashMap<String,Object> findAllById(Integer id){
        return  tBizArtDAO.findAllById(id);
    }

}
